var Base64Util = twc.utils.Base64Util;
MA = {};
MA.tracking = {

    values: {
        time: null, //time
        day: null, //day
        dow: null, //day of week
        cust_str: 'ebpp:'
    },

    steps: {
        'enter amount':'step 2',
        'verify':'step 3'
    },

    sendTrackRequest: function(object){

        var parent_url = decodeURIComponent( document.location.hash.replace( /^#/, '' ) );
        var encode = encodeURIComponent(Base64Util.encode(JSON.stringify(object)) );
        $.postMessage({ method:'trackObp', message:encode }, parent_url, parent );
        //JSON.stringify(encode)
    },

    error: function(errorMessage){
        var trackObject = {};
        trackObject["type"] = "linkTrack";
        trackObject["linkTrackVars"] = 'eVar54,events';
        trackObject["linkTrackEvents"] = 'event82';
        trackObject["events"] = 'event82';
        trackObject["eVar54"] = '{'+MA.tracking.get_page_name()+',unknown}:{'+errorMessage+'}';

        this.sendTrackRequest(trackObject);
    },

    tooltip: function(tooltip){
        var trackObject = {
            "type" : "trackLight",
            "pageName": MA.tracking.get_page_name(),
            "eVar57": MA.tracking.values.cust_str+'tooltip:{'+tooltip+'}',
            "eVar59": MA.tracking.get_page_name(),
            "lightTrackVars":"eVar57,eVar59"
        }
        this.sendTrackRequest(trackObject);
    },

    link_click: function(type, location){
        var trackObject = {};
        trackObject["type"] = "cookieLink"
        trackObject["value"] = '{ebpp:'+type+' now:'+location+'}';

        this.sendTrackRequest(trackObject);
    },

    track_step: function(step){
        var trackObject = {
            "type" : "page",
            "pageName": 'ebpp > bill pay > '+ MA.tracking.steps[step]+" / "+ step,
            "eVar2":MA.tracking.get_eVar2()
            //"pageName": MA.tracking.get_page_name() + " / "+ step,
        };
        this.sendTrackRequest(trackObject);
    },

    track_step_thank_you: function(){
        var value = ($('#payment_due').is(':checked')) ? $('#payment_due_value').text() : $('#payment_amount_value').val();
        var trackObject = {
            "type" : "page",
            "pageName": 'ebpp > bill pay > step 4'+' / thank you',
            "products": ';;;;event66='+value+'',
            "eVar4":MA.tracking.get_page_name().replace("|", "<"),
            "eVar2":MA.tracking.get_eVar2(),
            "events":"event66,event60"
            //MA.tracking.get_page_name() + " | thank you"
        }
        this.sendTrackRequest(trackObject);
    },

    get_eVar2: function(){
        if(typeof twc != 'undefined'){
            var paperless =  (twc.omniture.paperless == "true")? "paperless":"not paperless";
            var fullAccess = (twc.omniture.cpniVerified)? "full access":"limited";
            var v2 = twc.omniture.userRole + " > " + MA.tracking.get_page_name() + " > " + paperless + " > " + fullAccess + " > logged in";
            return v2;
        }else{
            return "unknown > unknown | unknown > unknown > unknown > unknown > unknown > unknown";
        }
    },

    get_page_name: function(){


        var url = $('#data_form').attr('action');

        if(url == undefined){
            return "server error";
        }

        if(url.indexOf("bank/onetime") != -1)
            return "one time | ach";

        if(url.indexOf("bank/reoccurring") != -1)
            return "recurring | ach";

        if(url.indexOf("card/onetime") != -1)
            return "one time | "+ MA.tracking.get_card_type();

        if(url.indexOf("card/reoccurring") != -1)
            return "recurring | "+ MA.tracking.get_card_type();

        return "unknown";

    },

    get_card_type: function(){
        var type = $('input[name="creditCardInfo.cardProcessingNetwork"]:checked').val();
        if(type === "D"){
            return "dc";
        }else if(type === "C"){
            return "cc";
        }
        return "unknown";
    }
};

$(document).ready(function(){
});